<template>
  <div class="register-container">
    <div class="register-card">
      <div class="register-header">
        <div class="register-logo">AI</div>
        <h1 class="register-title">FinanceAI</h1>
        <p class="register-subtitle">智能金融问答与分析系统</p>
      </div>
      
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label class="form-label">用户名</label>
          <input 
            type="text" 
            class="form-input" 
            placeholder="请输入用户名" 
            v-model="registerForm.username"
            required
          >
        </div>
        
        <div class="form-group">
          <label class="form-label">邮箱</label>
          <input 
            type="email" 
            class="form-input" 
            placeholder="请输入邮箱" 
            v-model="registerForm.email"
            required
          >
        </div>
        
        <div class="form-group">
          <label class="form-label">密码</label>
          <input 
            type="password" 
            class="form-input" 
            placeholder="请输入密码" 
            v-model="registerForm.password"
            required
          >
        </div>
        
        <div class="form-group">
          <label class="form-label">确认密码</label>
          <input 
            type="password" 
            class="form-input" 
            placeholder="请再次输入密码" 
            v-model="registerForm.confirmPassword"
            required
          >
        </div>
        
        <button type="submit" class="btn btn-primary btn-register" :disabled="loading">
          <span v-if="!loading">注册账户</span>
          <span v-else>注册中...</span>
        </button>
        
        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
        
        <div v-if="successMessage" class="success-message">
          {{ successMessage }}
        </div>
      </form>
      
      <div class="register-footer">
        <p>已经有账户了？<router-link to="/" class="login-link">立即登录</router-link></p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data() {
    return {
      registerForm: {
        username: '',
        email: '',
        password: '',
        confirmPassword: ''
      },
      loading: false,
      errorMessage: '',
      successMessage: ''
    };
  },
  methods: {
    async handleRegister() {
      // 重置消息
      this.errorMessage = '';
      this.successMessage = '';
      
      // 简单验证密码确认
      if (this.registerForm.password !== this.registerForm.confirmPassword) {
        this.errorMessage = '两次输入的密码不一致';
        return;
      }
      
      this.loading = true;
      
      try {
        const response = await fetch('http://localhost:8000/api/register', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            username: this.registerForm.username,
            email: this.registerForm.email,
            password: this.registerForm.password
          })
        });
        
        const data = await response.json();
        
        if (response.ok) {
          // 注册成功
          this.successMessage = '注册成功！即将跳转到登录页面...';
          setTimeout(() => {
            this.$router.push('/');
          }, 2000);
        } else {
          // 注册失败，显示错误信息
          this.errorMessage = data.detail || '注册失败，请稍后重试';
        }
      } catch (error) {
        console.error('Registration failed:', error);
        this.errorMessage = '网络错误，请稍后重试';
      } finally {
        this.loading = false;
      }
    }
  }
};
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
  width: 100%;
  height: 100vh;
  margin: 0;
  padding: 0;
}

.register-container::before {
  content: '';
  position: absolute;
  width: 300%;
  height: 100%;
  top: auto;
  bottom: -40%;
  left: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320"><path fill="white" fill-opacity="0.1" d="M0,128L48,138.7C96,149,192,171,288,170.7C384,171,480,149,576,160C672,171,768,213,864,213.3C960,213,1056,171,1152,149.3C1248,128,1344,128,1392,128L1440,128L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path></svg>') repeat-x bottom;
  background-size: 50% 100%;
  animation: wave 15s infinite linear;
}

@keyframes wave {
  0% { transform: translateX(0); }
  100% { transform: translateX(-25%); }
}

.register-card {
  background: white;
  border-radius: 20px;
  box-shadow: 0 15px 50px rgba(0, 0, 0, 0.15);
  width: 100%;
  max-width: 450px;
  padding: 3rem;
  position: relative;
  z-index: 1;
  animation: slideUp 0.5s ease;
}

.register-header {
  text-align: center;
  margin-bottom: 2rem;
}

.register-logo {
  width: 80px;
  height: 80px;
  margin: 0 auto 1rem;
  background: linear-gradient(135deg, #4A90E2, #764ba2);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 2rem;
  font-weight: bold;
}

.register-title {
  font-size: 1.8rem;
  color: #2C3E50;
  margin-bottom: 0.5rem;
}

.register-subtitle {
  color: #6c757d;
  font-size: 0.95rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-label {
  display: block;
  margin-bottom: 0.5rem;
  color: #2C3E50;
  font-weight: 500;
}

.form-input {
  width: 100%;
  padding: 0.75rem;
  border: 2px solid #DEE2E6;
  border-radius: 10px;
  font-size: 1rem;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #4A90E2;
}

.btn-register {
  width: 100%;
  padding: 0.875rem;
  background: linear-gradient(135deg, #4A90E2, #764ba2);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 1rem;
}

.btn-register:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 30px rgba(74, 144, 226, 0.3);
}

.btn-register:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.error-message {
  color: #D9534F;
  text-align: center;
  margin-top: 1rem;
  padding: 0.5rem;
  background-color: rgba(217, 83, 79, 0.1);
  border-radius: 5px;
}

.success-message {
  color: #5cb85c;
  text-align: center;
  margin-top: 1rem;
  padding: 0.5rem;
  background-color: rgba(92, 184, 92, 0.1);
  border-radius: 5px;
}

.register-footer {
  text-align: center;
  margin-top: 1.5rem;
  color: #6c757d;
}

.login-link {
  color: #4A90E2;
  text-decoration: none;
  font-weight: 500;
}

.login-link:hover {
  text-decoration: underline;
}
</style>